package com.alibaba.citrus.cr.platform.enhance.facade.service.scitem.group;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.platform.enhance.functions.item.group.ScItemGroupEntity;
import com.alibaba.citrus.cr.platform.enhance.repository.ScItemGroupRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.middleware.cache.runtime.service.EpochCacheService;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.bcorder.contants.BcOrderConstants;
import com.epoch.app.crplatformenhance.api.group.scitemgroupquery.service.ScItemGroupQueryService;
import com.epoch.app.crplatformenhance.dto.request.IdQueryRequest;
import com.epoch.app.crplatformenhance.dto.request.QueryScItemGroupRequest;
import com.epoch.app.crplatformenhance.dto.response.ScItemGroupResponse;
import com.epoch.app.crplatformenhance.sdo.ScItemGroupSDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * @author zls
 * @description: TODO
 * @date 2021/9/7 15:30
 */
@Primary
@Service
public class ScItemGroupQueryServiceImpl implements ScItemGroupQueryService {

    @Resource
    private ScItemGroupRepository scItemGroupRepository;

    @Resource
    private EpochCacheService epochCacheService;

    private final static String ITEM_GROUP_PREFIX = "item_group_prefix_";

    /**
     * 只支持id搜索缓存
     * @param request
     * @return
     */
    @Override
    public Result<ScItemGroupResponse> getScItemGroupById(IdQueryRequest request) {
        ScItemGroupResponse response;
        String jsonStr = epochCacheService.get(BcOrderConstants.REDIS_CACHE_CODE(), ITEM_GROUP_PREFIX + request.getId());
        if (StringUtils.isBlank(jsonStr)) {
            ScItemGroupSDO scItemGroupSDO = new ScItemGroupSDO();
            scItemGroupSDO.setId(request.getId());
            ScItemGroupEntity scItemGroupEntity = ScItemGroupEntity.of(scItemGroupSDO);
            response = scItemGroupEntity.doQueryById(request);
            if (null != response) {
                // 缓存设置1年
                epochCacheService.put(BcOrderConstants.REDIS_CACHE_CODE(), ITEM_GROUP_PREFIX + request.getId(), JSONObject.toJSONString(response), 365L, TimeUnit.DAYS);
            }
        } else {
            response = JSONObject.parseObject(jsonStr, ScItemGroupResponse.class);
        }

        return Result.success(response);
    }

    @Override
    public Result<ScItemGroupResponse> getScItemGroup(QueryScItemGroupRequest request) {
        ScItemGroupSDO scItemGroupSDO = new ScItemGroupSDO();
        BeanUtils.copyProperties(request, scItemGroupSDO);
        ScItemGroupEntity scItemGroupEntity = ScItemGroupEntity.of(scItemGroupSDO);
        return Result.success(scItemGroupEntity.doQuery(request));
    }

    @Override
    public Result<List<ScItemGroupSDO>> pageQueryScItemGroupList(QueryScItemGroupRequest request) {
        PageResult<List<ScItemGroupSDO>> pageResult = scItemGroupRepository.pageQueryScItemGroupList(request);
        return Result.listSuccess(pageResult.getContent(), pageResult.getTotal());
    }
}
